﻿using System;
using System.Collections.Generic;
using Microsoft.AspNet.SignalR.Hubs;
using System.Linq;
using ZSoft.PushServices.Net.DTO;

namespace ZSoft.PushServices.Net.Transport.ConnectionEvents
{
    public static class ConnectionEventManager
    {
        private static IList<IConnectionEventHandler> _handlers;

        private static IList<IConnectionEventHandler> Handlers
        {
            get
            {
                if (_handlers == null)
                {
                    _handlers = new List<IConnectionEventHandler>();
                }

                return _handlers;
            }
        }

        public static void RegisterHandler(IConnectionEventHandler handler)
        {
            if (handler == null)
            {
                throw new ArgumentException("handler can not be null");
            }

            Handlers.Add(handler);
        }


        internal static void ExecuteHandlers(ConnectionEventTypes handlerType, HubCallerContext context, ClientInfoDTO info)
        {
            Handlers.Where(h => h.Type == handlerType).ForEach(h => h.Execute(context, info));
        }
    }
}
